/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.view;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RoundProgressBar;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.yc.video.ResourceTable;
import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.player.VideoViewManager;

/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/11/9
 *     desc  : 预加载准备播放页面视图
 *     revise:
 * </pre>
 */
public class CustomPrepareView extends StackLayout implements InterControlView {
    private ControlWrapper mControlWrapper;
    private Image mIvThumb;
    private Component mIvStartPlay;
    private RoundProgressBar mPbLoading;
    private StackLayout mFlNetWarning;
    private Text mTvStart;

    public CustomPrepareView(Context context) {
        super(context);
        init(context);
    }

    public CustomPrepareView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomPrepareView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        Component view = LayoutScatter.getInstance(getContext()).parse(
            ResourceTable.Layout_custom_video_player_prepare, this, true);
        initFindViewById(view);
        initListener();
    }

    private void initFindViewById(Component view) {
        mIvThumb = (Image) view.findComponentById(ResourceTable.Id_iv_thumb);
        mIvStartPlay = view.findComponentById(ResourceTable.Id_iv_start_play);
        mPbLoading = (RoundProgressBar) view.findComponentById(ResourceTable.Id_pb_loading);
        mFlNetWarning = (StackLayout) view.findComponentById(ResourceTable.Id_fl_net_warning);
        mTvStart = (Text) view.findComponentById(ResourceTable.Id_tv_start);
        mPbLoading.setIndeterminate(true);
        mPbLoading.setProgressColor(Color.GRAY);
    }

    private void initListener() {
        mTvStart.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                mFlNetWarning.setVisibility(HIDE);
                VideoViewManager.instance().setPlayOnMobileNetwork(true);
                mControlWrapper.start();
            }
        });
    }

    /**
     * 设置点击此界面开始播放
     */
    public void setClickStart() {
        setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                mControlWrapper.start();
            }
        });
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayStateChanged(int playState) {
        switch (playState) {
            case ConstantKeys.CurrentState.STATE_PREPARING:
                moveChildToFront(this);
                setVisibility(VISIBLE);
                mIvStartPlay.setVisibility(HIDE);
                mFlNetWarning.setVisibility(HIDE);
                mPbLoading.setVisibility(VISIBLE);
                break;
            case ConstantKeys.CurrentState.STATE_PLAYING:
            case ConstantKeys.CurrentState.STATE_PAUSED:
            case ConstantKeys.CurrentState.STATE_ERROR:
            case ConstantKeys.CurrentState.STATE_COMPLETED:
            case ConstantKeys.CurrentState.STATE_BUFFERING_PAUSED:
            case ConstantKeys.CurrentState.STATE_BUFFERING_PLAYING:
            case ConstantKeys.CurrentState.STATE_ONCE_LIVE:
                setVisibility(HIDE);
                break;
            case ConstantKeys.CurrentState.STATE_IDLE:
                setVisibility(VISIBLE);
                moveChildToFront(this);
                mPbLoading.setVisibility(HIDE);
                mFlNetWarning.setVisibility(HIDE);
                mIvStartPlay.setVisibility(VISIBLE);
                mIvThumb.setVisibility(VISIBLE);
                break;
            case ConstantKeys.CurrentState.STATE_START_ABORT:
                setVisibility(VISIBLE);
                mFlNetWarning.setVisibility(VISIBLE);
                mFlNetWarning.moveChildToFront(this);
                break;
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
    }

    @Override
    public void setProgress(int duration, int position) {
    }

    @Override
    public void onLockStateChanged(boolean isLocked) {
    }

    public Image getThumb() {
        return mIvThumb;
    }
}
